import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()        
        #layout.setAlignment(Qt.AlignmentFlag.AlignTop)

        toolBar =QToolBar()

        toolBar.addWidget(QLabel('FontFilter:'))
        self.filter = QComboBox()
        self.filter.addItems(QFontComboBox.FontFilter.__members__.keys())
        self.filter.currentTextChanged.connect(self.OnFontFilterChanged)
        toolBar.addWidget(self.filter)        

        toolBar.addWidget(QLabel('WritingSystem:'))
        self.system = QComboBox()
        self.system.addItems(QFontDatabase.WritingSystem.__members__.keys())
        self.system.currentTextChanged.connect(self.OnWritingSystemChanged)
        toolBar.addWidget(self.system)

        self.font = QFontComboBox()
        self.font.currentFontChanged.connect(self.CurrentFontChanged)
        toolBar.addWidget(self.font)

        toolBar.addWidget(QLabel('FontWeight:'))
        self.fontWeight = QComboBox()
        self.fontWeight.addItems(QFont.Weight.__members__.keys())
        self.fontWeight.currentTextChanged.connect(self.OnFontWeightChanged)
        toolBar.addWidget(self.fontWeight)

        toolBar.addWidget(QLabel('FontSize:'))
        self.fontSize = QComboBox()
        self.fontSize.addItems([str(i) for i in range(5,40,2)])
        self.fontSize.currentTextChanged.connect(self.OnFontSizeChanged)
        toolBar.addWidget(self.fontSize)

        cb = QCheckBox('Italic')
        cb.stateChanged.connect(self.OnItalicChanged)
        toolBar.addWidget(cb)

        cb = QCheckBox('Underline')
        cb.stateChanged.connect(self.OnUnderlineChanged)
        toolBar.addWidget(cb)

        layout.addWidget(toolBar)        

        self.text=QTextEdit()
        self.text.setText('字体效果展示')
        self.text.setReadOnly(True)       
        self.text.selectAll()#不选中设置没效果
        self.text.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(self.text)

        self.setLayout(layout) 
    def OnFontFilterChanged(self,text):
        filter = QFontComboBox.FontFilter[text]
        self.font.setFontFilters(filter)
        self.parent.OnShowMessage(f'Set FontFilter: {filter}')
    def OnWritingSystemChanged(self,text):
        system=QFontDatabase.WritingSystem[text]
        self.font.setWritingSystem(system)
        self.parent.OnShowMessage(f'Set WritingSystem: {system}')
    
    def CurrentFontChanged(self,font):
        family = font.family()        
        self.parent.OnShowMessage(f'Set Font: {family}')
        self.text.setFont(font)
    def OnFontWeightChanged(self,text):
        fontWeight=QFont.Weight[text]
        self.text.setFontWeight(fontWeight)
        self.parent.OnShowMessage(f'Set FontWeight: {fontWeight}')
    def OnFontSizeChanged(self,text):
        fontSize=int(text)
        self.text.setFontPointSize(fontSize)
        self.parent.OnShowMessage(f'Set FontPointSize: {fontSize}')
    def OnItalicChanged(self,state):
        self.text.setFontItalic(state)
        self.parent.OnShowMessage(f"Set FontItalic={state}")
    def OnUnderlineChanged(self,state):
        self.text.setFontUnderline(state)
        self.parent.OnShowMessage(f"Set FontUnderline={state}")
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet